package pres.seanlang.utility.android;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import pres.seanlang.utility.StreamUtils;
import pres.seanlang.utility.PropertyUtils;

/**
 *  Assets工具类
 *
 * @author SeanLang
 */
public class AAssetUtils {
    private static final String TAG = "AssetUtils";

    protected AAssetUtils() { throw new UnsupportedOperationException("can't instance..."); }

    /**
     *  获取Assets中指定文件的InputStream
     * @param context
     * @param fileName
     * @return
     */
    public static InputStream get(Context context, String fileName) {
        InputStream in = null;
        AssetManager assets = context.getAssets();
        try {
            in = assets.open(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return in;
    }

    /**
     *  获取Assets中指定文件的bytes
     * @param context
     * @param fileName
     * @return
     */
    public static byte[] getAsBytes(Context context, String fileName) {
        InputStream in = get(context, fileName);
        byte[] bytes = StreamUtils.readFully(in);
        if(bytes != null) {
            return bytes;
        } else {
            return new byte[]{};
        }
    }

    /**
     * 获取Assets中指定文件的Text
     * @param context
     * @param fileName
     * @return
     */
    public static String getAsText(Context context, String fileName) {
        InputStream in = get(context, fileName);
        return StreamUtils.readFullyAsText(in);
    }

    /**
     *  获取Assets中指定文件的Properties实例
     * @param context
     * @param fileName
     * @return
     */
    public static Properties getAsProperties(Context context, String fileName) {
        InputStream inputStream = get(context, fileName);
        return PropertyUtils.create(inputStream);
    }

    /**
     *  从Assets的Properties文件中获取指定值
     * @param context
     * @param fileName
     * @param key
     * @param def
     * @return
     */
    public static String getStringFromProperties(Context context, String fileName, String key, String def) {
        InputStream in = get(context, fileName);
        return PropertyUtils.readString(in, key, def);
    }

    /**
     * 从Assets的Properties文件中获取指定值
     * @param context
     * @param fileName
     * @param key
     * @param def
     * @return
     */
    public static long getIntFromProperties(Context context, String fileName, String key, long def) {
        InputStream in = get(context, fileName);
        return PropertyUtils.readLong(in, key, def);
    }

    /**
     * 从Assets的Properties文件中获取指定值
     * @param context
     * @param fileName
     * @param key
     * @param def
     * @return
     */
    public static double getDoubleFormProperties(Context context, String fileName, String key, double def) {
        InputStream in = get(context, fileName);
        return PropertyUtils.readDouble(in, key, def);
    }

    /**
     *  获取Assets中指定文件的bitmap
     * @param context
     * @param fileName
     * @return
     */
    public static Bitmap getAsBitmap(Context context, String fileName) {
        InputStream in = get(context, fileName);
        return BitmapFactory.decodeStream(in);
    }
}
